package com.share;

import com.share.entity.User;
import sun.misc.VM;

import java.lang.ref.SoftReference;
import java.lang.ref.WeakReference;

/**
 * @author sunxuejian
 * @date 2020/4/7
 */
public class ReferenceTest {

    public static void main(String[] args) throws InterruptedException {

        //-Xms25m -Xmx25m -XX:NewRatio=4 -XX:SurvivorRatio=8 -XX:+PrintGCDetails -XX:+UseSerialGC

        //-XX:MaxDirectMemorySize=60M  -Xms25m -Xmx25m
        System.out.println(Runtime.getRuntime().maxMemory()); //25m
        System.out.println(VM.maxDirectMemory());             //60m


        //-Xms25m -Xmx25m
        System.out.println(Runtime.getRuntime().maxMemory()); //25m
        System.out.println(VM.maxDirectMemory());             //25m

    }

    private static void test111() throws InterruptedException {
        User user = new User();
        user.setAge(29);

        WeakReference<User> uwf = new WeakReference<User>(user);
        System.out.println(uwf.get().getAge());

        user = null;

        byte[] b1 = new byte[1*1024*1024];
//        byte[] b2 = new byte[1*1024*1024];
//        byte[] b3 = new byte[1*1024*1024];
//        byte[] b4 = new byte[1*1024*1024];
//        byte[] b5 = new byte[1*1024*1024];

        Thread.sleep(3000);

        System.out.println(uwf.get());
    }

    private static void SoftRef2() {
        System.out.println("start");
        User obj = new User();
        obj.setNaem("sunxj");
        SoftReference<User> sr = new SoftReference<User>(obj);
        obj = null;
        System.out.println(sr.get());
        System.out.println("end");
    }

    private static void oom() {
        Object object=new Object();
        Object[] objArr=new Object[Integer.MAX_VALUE];
    }

    private static void softRef() throws InterruptedException {
        User user = new User();
        user.setAge(29);

        SoftReference<User> usf = new SoftReference<User>(user);
        System.out.println(usf.get().getAge());

        user = null;
        System.gc();
        Thread.sleep(3000);

        System.out.println(usf.get());
    }
}
